package com.xhb.ui.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

import com.xhb.ui.http.HttpServer;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;

public class CmdUtil {

	
	/**
	 * 执行一个cmd命令
	 *
	 * @param cmdCommand cmd命令
	 * @return 命令执行结果字符串，如出现异常返回null
	 * @throws Exception
	 */
	public static String executeCmdCommand(String cmdCommand)  {

		File tmpBat=null;
		try {
			tmpBat = File.createTempFile(UUID.fastUUID().toString(), ".bat");
		} catch (IOException e) {
			e.printStackTrace();
		}

		FileUtil.writeString(cmdCommand, tmpBat, "GBK");

		StringBuilder stringBuilder = new StringBuilder();
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("cmd /c start " + tmpBat.getPath());
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				LogFormat.info(line);
				stringBuilder.append(line).append(" ");
			}
			LogFormat.info(stringBuilder.toString());
			LogFormat.info("executeCmdCommand OK");
			return stringBuilder.toString();
		} catch (Exception e) {
			e.printStackTrace();
			LogFormat.info(e.getMessage());
			return null;
		}

	}

	public static void main(String [] args) {
		
		File dir = new File("C:\\zgymw\\浙江学员名单\\周建斌-装饰装修施工员-330721197303086015\\n");
		if (!dir.exists()) {
			JOptionPane.showMessageDialog(null, "请先生成背景图");
			return;
		}
		if (dir.listFiles().length == 0) {
			JOptionPane.showMessageDialog(null, "请先生成背景图");
			return;
		}

		// 生成视频
		for (File f : dir.listFiles()) {
			
			String resultPath="C:\\zgymw\\浙江学员名单\\周建斌-装饰装修施工员-330721197303086015" + "/video/" + f.getName();
			
			File result=new File(resultPath+"/result.mp4");
			if(result.exists()) {
				continue;
			}

			String cmd = "activate.bat && conda activate paddle_env && cd /d " + HttpServer.BASE_PATH
					+ "//PaddleGAN//applications && python -u tools/first-order-demo.py   --driving_video "
					+ HttpServer.BASE_PATH + "//3.MOV   --source_image " + f.getPath()
					+ "  --relative   --adapt_scale     --output " + 
					resultPath + "      --image_size 512    --face_enhancement";

			LogFormat.info("cmd : " + cmd);

			// 调用执行视频命令
			CmdUtil.executeCmdCommand(cmd);
		}
		
		//CmdUtil.executeCmdCommand("activate.bat && conda activate paddle_env && cd /d C://zgymw//PaddleGAN//applications && python -u tools/first-order-demo.py   --driving_video C://zgymw//3.MOV   --source_image C:\\zgymw\\浙江学员名单\\周建斌-装饰装修施工员-330721197303086015\\n\\new6.png  --relative   --adapt_scale     --output C:\\zgymw\\浙江学员名单\\周建斌-装饰装修施工员-330721197303086015/video/new6.png      --image_size 512    --face_enhancement");
		//CmdUtil.executeCmdCommand("cmd /c start " + HttpServer.BASE_PATH +"//GEN_video.bat");
	//	CmdUtil.executeCmdCommand("cmd conda activate paddle_env");
		//CmdUtil.executeCmdCommand("cmd cd C://zgymw//PaddleGAN//applications");
		
		
	}
}
